<?php

use console\db\channels\TransferTableMigration;

/**
 * 圈子视频表迁移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300020_000006_t_sns_dynamic_video extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public function validateTransfer(&$data) {
        if (!parent::validateTransfer($data)) {
            return false;
        }
        if ($data['duration'] <= 0) {
            return false;
        }
        if ($data['deleted_at']) {
            return false;
        }
        return true;
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'sns_dynamic_video' => 'sns_videos',
            ],
            'columns' => [
                'video_id' => 'id',
                'user_id' => 'uid',
                'duration',
                'path' => function($data) {
                    if ($data['filename']) {
                        if (strpos($data['filename'], 'http') !== false) {
                            return $data['filename'];
                        }
                        return '/uploads/' . ltrim($data['filename'], '/');
                    }
                    return '';
                },
                'resolution' => function($data) {
                    if ($data['height'] > 0 && $data['width'] > 0) {
                        return $data['width'] / $data['height'];
                    }
                    return 1;
                },
                'created_at' => function($data) {
                    if ($data['created_at']) {
                        return strtotime($data['created_at']);
                    }
                    return 0;
                },
                'updated_at' => function($data) {
                    if ($data['updated_at']) {
                        return strtotime($data['updated_at']);
                    }
                    return 0;
                },
                'deleted_at' => function($data) {
                    if ($data['deleted_at']) {
                        return strtotime($data['deleted_at']);
                    }
                    return 0;
                },
            ],
        ];
    }

}
